package com.itheima.seach;

public class BlockSearch {
    public static void main(String[] args) {
        int[] arr = {16, 5, 9, 12, 21, 18,
                32, 23, 37, 26, 45, 34,
                50, 48, 61, 52, 73, 66};
        Block b1 = new Block(21, 0, 5);
        Block b2 = new Block(45, 6, 11);
        Block b3 = new Block(73, 12, 17);

        Block[] blockArr = {b1, b2, b3};
        //定义要查找的数据
        int number = 66;


        int index=getIndex(arr,blockArr,number);
        System.out.println(index);
    }

    private static int getIndex(int[] arr, Block[] blockArr, int number) {
        //定义方法，确定要查找的元素在哪个块当中
        int blockIndex=getBlockIndex(blockArr,number);
        if(blockIndex==-1){
            return -1;
        }
        Block b = blockArr[blockIndex];
        int startIndex = b.getStartIndex();
        int endIndex = b.getEndIndex();
        for (int i = startIndex; i <= endIndex; i++) {
            if(arr[i]==number){
                return i;
            }
        }
        return -1;
    }

    private static int getBlockIndex(Block[] blockArr, int number) {
        for (int i = 0; i < blockArr.length; i++) {
            Block b = blockArr[i];
            int max = b.getMax();
            if (number<=max){
                return i;
            }
        }
        return -1;
    }
}

class Block {
    private int max;
    private int startIndex;
    private int endIndex;

    public Block() {
    }

    public Block(int max, int startIndex, int endIndex) {
        this.max = max;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public int getEndIndex() {
        return endIndex;
    }

    public void setEndIndex(int endIndex) {
        this.endIndex = endIndex;
    }
}
